import subprocess
import sys
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.common.exceptions import WebDriverException

def check_chrome_installation():
    """检查Chrome浏览器是否安装"""
    # 检查常见的Chrome安装路径
    chrome_paths = [
        '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
        '/usr/bin/google-chrome',
        '/usr/bin/chromium-browser',
        'google-chrome'
    ]
    
    for path in chrome_paths:
        try:
            if os.path.exists(path) or subprocess.run(['which', path.split('/')[-1]], 
                                                     capture_output=True, text=True).returncode == 0:
                print("Chrome浏览器已安装，路径:", path)
                return path
        except:
            continue
    
    print("Chrome浏览器未找到")
    return None

def check_firefox_installation():
    """检查Firefox浏览器是否安装"""
    try:
        result = subprocess.run(['which', 'firefox'], capture_output=True, text=True)
        if result.returncode == 0:
            print("Firefox浏览器已安装，路径:", result.stdout.strip())
            return True
        else:
            print("Firefox浏览器未找到")
            return False
    except Exception as e:
        print(f"检查Firefox安装时出错: {e}")
        return False

def check_chromedriver():
    """检查ChromeDriver是否可用"""
    try:
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        
        print("正在尝试启动ChromeDriver...")
        driver = webdriver.Chrome(options=chrome_options)
        print("ChromeDriver启动成功")
        driver.quit()
        print("ChromeDriver关闭成功")
        return True
    except WebDriverException as e:
        print(f"ChromeDriver启动失败: {e}")
        return False
    except Exception as e:
        print(f"检查ChromeDriver时发生未知错误: {e}")
        return False

def check_geckodriver():
    """检查GeckoDriver是否可用"""
    try:
        firefox_options = FirefoxOptions()
        firefox_options.add_argument('--headless')
        
        print("正在尝试启动GeckoDriver...")
        driver = webdriver.Firefox(options=firefox_options)
        print("GeckoDriver启动成功")
        driver.quit()
        print("GeckoDriver关闭成功")
        return True
    except WebDriverException as e:
        print(f"GeckoDriver启动失败: {e}")
        return False
    except Exception as e:
        print(f"检查GeckoDriver时发生未知错误: {e}")
        return False

def simple_selenium_test():
    """简单的Selenium测试"""
    print("=== 简单Selenium测试 ===")
    
    # 检查Chrome
    chrome_path = check_chrome_installation()
    if chrome_path:
        if check_chromedriver():
            print("Chrome环境配置正确，可以使用Chrome进行测试")
            return "chrome"
    
    # 检查Firefox
    if check_firefox_installation():
        if check_geckodriver():
            print("Firefox环境配置正确，可以使用Firefox进行测试")
            return "firefox"
    
    print("没有找到可用的浏览器和对应的WebDriver")
    return None

if __name__ == "__main__":
    result = simple_selenium_test()
    if result:
        print(f"推荐使用 {result} 浏览器进行Selenium测试")
    else:
        print("请安装Chrome或Firefox浏览器以及对应的WebDriver")